from fastapi import APIRouter, Cookie, status
from fastapi.responses import JSONResponse

from typing import Annotated

from src.user_service.service.auth import Auth_service
from src.user_service.schemas import (
    Login_form,
    Registration_form
)

auth_router = APIRouter()


@auth_router.post(
    "/registration",
    # response_model=My_account,
    status_code=status.HTTP_201_CREATED
)
async def registration_user(
    payload: Registration_form,
    id: Annotated[int | None, Cookie()] = None
):
    user_account = await Auth_service().create_user(
        payload,
        user_id=id
    )

    response = JSONResponse(
        status_code=201,
        content=user_account.model_dump()
    )
    response.set_cookie(
        key="id",
        value=user_account.id
    )
    return response


@auth_router.post(
    "/login",
    # response_model=id_schema,
    status_code=200
)
async def login_user(
    form_data: Login_form,
    id: Annotated[int | None, Cookie()] = None
):
    user_id = await Auth_service().login_user(
        form_data,
        user_id=id
    )
    response = JSONResponse(
        status_code=200,
        content={
            "id": user_id
        }
    )
    response.set_cookie(
        key="id",
        value=user_id
    )
    return response
